﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FanaticUnitity.FfmpegZoomPictureTo300KB.Helpers
{
    internal static class FileHelper
    {
        ///<summary>文件尺寸</summary>
        public const long SizeSmallKB = 200 * 1024;

        private const string CompressedSubDirName = "Compressed";

        ///<summary>是否文件尺寸大于300KB</summary>
        public static bool IsSizeLarger300KB(string filePath)
        {
            var fileInfo = new FileInfo(filePath);
            return fileInfo.Length > SizeSmallKB;
        }

        ///<summary>是否文件尺寸小于等于300KB</summary>
        public static bool IsSizeSmallerOrEqual300KB(string filePath)
        {
            return !IsSizeLarger300KB(filePath);
        }

        ///<summary>获得压缩后文件的路径。</summary>
        public static string GetCompressedFileName(string filePath)
        {
            var fileName = Path.GetFileName(filePath);
            var dirPath = Path.GetDirectoryName(filePath);

            return Path.Combine(dirPath, CompressedSubDirName, fileName);
        }

        public static void CreateCompressedDirectory(string filePath)
        {
            var fileName = Path.GetFileName(filePath);
            var dirPath = Path.GetDirectoryName(filePath);
            var compressedDirPath =  Path.Combine(dirPath, CompressedSubDirName);
            if (Directory.Exists(compressedDirPath)) return;
            Directory.CreateDirectory(compressedDirPath);
        }

        public static bool IsImageFormat(string filePath)
        {
            if (string.IsNullOrWhiteSpace(filePath)) return false;

            var extArr = new string[] 
            {
                ".png",
                ".jpg",
                ".jpeg",
            };

            return extArr.Any(ext => filePath.EndsWith(ext, StringComparison.OrdinalIgnoreCase));
        }
    }
}
